$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        dom: {
            search: null
        },
        start: null,
        end:null,
        init: function () {
            this.start = $('#plan_startTime').datebox({disabled: true, width: 95});
            this.end = $('#plan_endTime').datebox({disabled: true, width: 95});
            this.dataGrid = new xhDataGrid($('#list'), 'res/getScheduleSummaryData');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_handleStatus']);
            this.dom.search = $('#bt_list_search');
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.funObj().addCatalogData();//异步加载章节数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            //时间类型
            $('#search_date').combobox({
                valueField:'value',
                textField:'label',
                data: [
                    {label: '--选择时间--', value: ''},
                    {label: '申请开始', value: '0'},
                    {label: '申请结束', value: '1'},
                    {label: '计划开始', value: '2'},
                    {label: '计划结束', value: '3'},
                    {label: '申请开始-结束', value: '4'},
                    {label: '计划开始-结束', value: '5'}],
                onSelect: function(param){
                    var index = $.inArray(param.value, ['0', '1', '2', '3', '4', '5', '6']);
                    var _disabled = true;
                    if (index >= 0) {
                        _disabled = false;
                        var startTime = $this.start.getElementValue(false);
                        var endTime = $this.end.getElementValue(false);
                        $('#plan_startTime').datebox({
                            disabled: _disabled
                        }).datebox(
                            'setValue', startTime
                        );
                        $('#plan_endTime').datebox({
                            disabled: _disabled
                        }).datebox(
                            'setValue', endTime
                        );
                    } else {
                        $('#plan_startTime').datebox({
                            disabled: _disabled
                        });
                        $('#plan_endTime').datebox({
                            disabled: _disabled
                        });
                    }
                }
            });
            $('#plan_status').combobox({
                valueField:'value',
                textField:'label',
                onSelect: function(param){
                    var index = $.inArray(param.value, ['3']);
                    var _disabled = true;
                    if (index >= 0) {
                        _disabled = false;
                        $('#deal_status').combobox({
                            disabled: _disabled
                        });
                    } else {
                        $('#deal_status').combobox({
                            disabled: _disabled
                        });
                    }
                }
            });
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            });
            xhTool.bindDocKeyDown($this.dom.search);
            //双击
            this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                var iBookId = rowData['iBookId'];
                var sBookName = rowData['sBookName'];
                var footStr = '<button type="button" class="btn btn-default" data-dismiss="modal" id="bt_modal_close">关闭</button>';
                if (addPower) {
                    footStr = '<button type="button" class="btn btn-primary xh-btn-load" id="plan_form_confirm">保存</button>' + footStr;
                }
                window.top.xhTop.showModal({
                    title: '【排期计划列表】书名：' + sBookName,
                    id: 'plan_form',
                    footerHtml:footStr,
                    content: xhTool.getFrameString('res/applyPlanDetail', {type: 2, bookId: iBookId}, $(window).height()-80),
                    size: 'lg',
                    backClose: false
                });
            };
            //设置处理状态
            $('#bt_list_handleStatus').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().setHandleStatus();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //查找
                search: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague')),//查询
                            plan_status: xhExtra.getValue($('#plan_status')),//计划状态
                            deal_status: xhExtra.getValue($('#deal_status')),//处理状态
                            plan_date: $('#search_date').combobox('getValue'),//时间类型
                            startTime: $this.start.getElementValue(false),
                            endTime: $this.end.getElementValue(false)
                        }
                    })
                },
                addCatalogData: function () {
                    $this.dataGrid.onLoadSuccess = function(data){
                        var rows = $this.dataGrid.command('getRows');
                        var idArr = [];
                        $.each(rows, function (index, row) {
                            idArr.push(row.iId);
                            $this.dataGrid.command('updateRow', {index: index, row: {
                                    sDemand: '加载中..'
                                }});
                        });
                        xhExtra.ajaxRequest('res/getCatalogData', {idArr: idArr}, {}, function (data) {
                            if (data) {
                                for (var index in rows) {
                                    index = parseInt(index);
                                    $this.dataGrid.command('updateRow', {
                                        index: index,
                                        row: {
                                            sDemand: data[index]
                                        }
                                    });
                                }
                            }
                        });
                        //end add
                        xhExtra.checkError(data);
                        // $this.dataGrid.clear();
                    };
                },
                setHandleStatus: function () {
                    var iId = $this.dataGrid.getSelectValues('iId');
                    var statusName = $this.dataGrid.getSelectValue('statusName');
                    var dealStatusName = $this.dataGrid.getSelectValue('dealStatusName');
                    if (!iId.length) {
                        window.top.xhTop.showError('请选择后操作');
                        return false;
                    }
                    if (statusName !== '不同意') {
                        window.top.xhTop.showError("仅'不同意'状态可操作，请重新选择");
                        return false;
                    }
                    if (dealStatusName !== '未处理') {
                        window.top.xhTop.showError("该申请已被处理过，不可重复操作");
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写处理信息',
                        html: '<div>' +
                        '<textarea id="dealMsg" class="swal2-textarea" style="width: 100%" placeholder="必填" >',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var dealMsg = $('#dealMsg', window.top.document).val();
                                if(!dealMsg){
                                    reject('请填写信息');
                                } else {
                                    xhExtra.ajaxRequest('res/setPlanDealStatus', {
                                        id: iId,
                                        dealMsg: dealMsg
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                }
                            })
                        }
                    })
                }
            }
        }
    }
};

